/**
 * Project Name:mauto-web
 * File Name:ApiEnterpriseController.java
 * Package Name:com.inftech.mauto.sys.web.front
 * Date:2016年10月17日下午9:51:38
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.sys.entity.Enterprise;
import com.inftech.mauto.sys.service.EnterpriseService;
import com.mingsoft.weixin.entity.WeixinPeopleEntity;

import net.mingsoft.basic.util.BasicUtil;

/**
 * ClassName:EnterpriseController <br/>
 * Function: 服务单位. <br/>
 * Date: 2016年10月17日 下午9:51:38 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/${managerPath}/enterprise")
public class EnterpriseController extends BaseController {

	/**
	 * 注入服务单位业务层
	 */
	@Autowired
	protected EnterpriseService enterpriseService;

	/**
	 * 
	 * list:获取服务单位列表. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request) throws Exception {
		try {
			BasicUtil.startPage();
			Map<String, Object> whereMap = BasicUtil.assemblyRequestMap();
			// 获取服务单位列表
			List<Enterprise> enterpriseList = enterpriseService.queryEnterpriseList(whereMap);
			BasicUtil.endPage(enterpriseList);
			request.setAttribute("enterpriseList", enterpriseList);
			LOG.info("后台获取服务单位列表成功");
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("后台获取服务单位列表失败", e);
		}
		return view("/mauto/sys/enterprise_list");
	}

	/**
	 * 
	 * getPeople:根据用户昵称获取用户. <br/>
	 *
	 * @author puyf
	 * @param nickName
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getPeople", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getPeople(String nickName) throws Exception {
		String outJson = "";
		try {
			WeixinPeopleEntity people = enterpriseService.queryPeople(nickName);
			LOG.info("后台查询用户信息成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), people);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			if (e.getCause() instanceof TooManyResultsException) {
				// nothing todo
			} else {
				LOG.error("后台查询用户信息失败", e);
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * bindPeople:绑定用户. <br/>
	 *
	 * @author puyf
	 * @param enterpriseId
	 * @param peopleId
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/bindPeople", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String bindPeople(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			Map<String, Object> requestMap = BasicUtil.assemblyRequestMap();
			enterpriseService.modifyEnterpriseBind(requestMap);
			LOG.info("后台服务单位绑定管理员帐号成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("后台服务单位绑定管理员帐号失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}
}
